<template>
	<view :data-theme="theme">
		<view class='productList'>
			<view class='search bg_color acea-row row-between-wrapper'>
				<!-- #ifdef H5 -->
				<view class="iconfont icon-xiangzuo" @click="goback()"></view>
				<!-- #endif -->
				<view class='input acea-row row-between-wrapper'><text class='iconfont icon-sousuo'></text>
					<input placeholder='搜索商品名称' placeholder-class='placeholder' confirm-type='search' name="search"
						:value='where.keyword' @confirm="searchSubmit" maxlength="20" />
				</view>
				<view class='iconfont view-toggle' :class="isListView ? 'icon-liebiao' : 'icon-tupianpailie'" @click="toggleView">
				</view>
			</view>
			<view class='nav acea-row row-middle'>
				<view class='item' :class='title ? "font_color":""' @click='set_where(1)'>{{title ? title:'默认'}}</view>
				<view class='item' @click='set_where(2)'>
					价格
					<image v-if="price==1" :src="urlDomain+'crmebimage/perset/staticImg/up.png'"></image>
					<image v-else-if="price==2" :src="urlDomain+'crmebimage/perset/staticImg/down.png'"></image>
					<image v-else :src="urlDomain+'crmebimage/perset/staticImg/horn.png'"></image>
				</view>
				<view class='item' @click='set_where(3)'>
					销量
					<image v-if="stock==1" :src="urlDomain+'crmebimage/perset/staticImg/up.png'"></image>
					<image v-else-if="stock==2" :src="urlDomain+'crmebimage/perset/staticImg/down.png'"></image>
					<image v-else :src="urlDomain+'crmebimage/perset/staticImg/horn.png'"></image>
				</view>
				<!-- down -->
				<view class='item' :class='nows ? "font_color":""' @click='set_where(4)'>新品</view>
			</view>
			<view class='product-grid-container' :class="{'list-view': isListView}" v-if="productList.length>0">
				<view class='product-grid' :class="{'list-grid': isListView}">
					<view class='product-card' :class="{'list-card': isListView}" hover-class='none'
						v-for="(item,index) in productList" :key="index" @click="godDetail(item)">
						<view class='product-image-container'>
							<image :src='item.image' class='product-image'></image>
							<view class="new-tag" v-if="item.isNew">新品</view>
							<span class="activity-tag"
								v-if="item.activityH5 && item.activityH5.type === '1'">秒杀</span>
							<span class="activity-tag"
								v-if="item.activityH5 && item.activityH5.type === '2'">砍价</span>
							<span class="activity-tag"
								v-if="item.activityH5 && item.activityH5.type === '3'">拼团</span>
						</view>
						<view class='product-info'>
							<view class='product-name'>{{item.storeName}}</view>
							<view class='product-desc'>{{item.info}}</view>
							<view class='product-tags'>
								<text class='tag' v-for="tag in item.tags" :key="tag">{{tag}}</text>
							</view>
							<view class='product-actions'>
								<view class='action-btn inquiry-btn' @click.stop="goToInquiry(item)">在线询价</view>
								<view class='action-btn quote-btn' @click.stop="goToQuote(item)">在线报价</view>
							</view>
						</view>
					</view>
				</view>
				<view class='loadingicon acea-row row-center-wrapper' v-if='productList.length > 0'>
					<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
				</view>
			</view>
		</view>
		<view class='noCommodity' v-if="productList.length==0 && where.page > 1">
			<view class='pictrue'>
				<image :src="urlDomain+'crmebimage/perset/staticImg/noShopper.png'"></image>
			</view>
			<recommend ref="recommendIndex"></recommend>
		</view>
	</view>
</template>

<script>
	import {getProductslist,getProductHot} from '@/api/store.js';
	import recommend from '@/components/recommend';
	import {mapGetters} from "vuex";
	import {goShopDetail} from '@/libs/order.js'
	import animationType from '@/utils/animationType.js'
	let app = getApp();
	export default {
		computed: mapGetters(['uid']),
		components: {
			recommend
		},
		data() {
			return {
				urlDomain: this.$Cache.get("imgHost"),
				productList: [],
				where: {
					keyword: '',
					priceOrder: '',
					salesOrder: '',
					news: 0,
					page: 1,
					limit: 20,
					cid: '',
				},
				price: 0,
				stock: 0,
				nows: false,
				loadend: false,
				loading: false,
				loadTitle: '加载更多',
				title: '',
				theme:app.globalData.theme,
				isListView: false // 视图模式：false=网格视图，true=列表视图
			};
		},
		onLoad: function(options) {
			this.$set(this.where, 'cid', options.cid || '');
			this.title = options.title || '';
			this.$set(this.where, 'keyword', options.searchValue || '');
			// 加载保存的视图偏好
			const savedViewMode = uni.getStorageSync('product_view_mode');
			this.isListView = savedViewMode === 'list';
			this.get_product_list();
		},
		methods: {
			goback() {
				// #ifdef H5
				return history.back();
				// #endif
				// #ifndef H5
				return uni.navigateBack({
					delta: 1,
				})
				// #endif
			},
			// 去详情页
			godDetail(item) {
				goShopDetail(item, this.uid).then(res => {
					uni.navigateTo({
						animationType: animationType.type,						animationDuration: animationType.duration,
						url: `/pages/goods/goods_details/index?id=${item.id}`
					})
				})
			},
			// 去询价页面
			goToInquiry(item) {
				uni.navigateTo({
					url: `/pages/goods/inquiry/inquiry?productId=${item.id}&productName=${encodeURIComponent(item.storeName)}`
				})
			},
			// 去报价页面
			goToQuote(item) {
				uni.navigateTo({
					url: `/pages/goods/quotation/quotation?productId=${item.id}&productName=${encodeURIComponent(item.storeName)}`
				})
			},
			// 切换视图模式
			toggleView() {
				this.isListView = !this.isListView;
				// 保存视图偏好到本地存储
				uni.setStorageSync('product_view_mode', this.isListView ? 'list' : 'grid');
			},
			searchSubmit: function(e) {
				let that = this;
				that.$set(that.where, 'keyword', e.detail.value);
				that.loadend = false;
				that.$set(that.where, 'page', 1)
				this.get_product_list(true);
			},
			//点击事件处理
			set_where: function(e) {
				switch (e) {
					case 1:
						return;
						break;
					case 2:
						if (this.price == 0) this.price = 1;
						else if (this.price == 1) this.price = 2;
						else if (this.price == 2) this.price = 0;
						this.stock = 0;
						break;
					case 3:
						if (this.stock == 0) this.stock = 1;
						else if (this.stock == 1) this.stock = 2;
						else if (this.stock == 2) this.stock = 0;
						this.price = 0
						break;
					case 4:
						this.nows = !this.nows;
						break;
				}
				this.loadend = false;
				this.$set(this.where, 'page', 1);
				this.get_product_list(true);
			},
			//设置where条件
			setWhere: function() {
				if (this.price == 0) this.where.priceOrder = '';
				else if (this.price == 1) this.where.priceOrder = 'asc';
				else if (this.price == 2) this.where.priceOrder = 'desc';
				if (this.stock == 0) this.where.salesOrder = '';
				else if (this.stock == 1) this.where.salesOrder = 'asc';
				else if (this.stock == 2) this.where.salesOrder = 'desc';
				this.where.news = this.nows ? 1 : 0;
			},
			//查找产品
			get_product_list: function(isPage) {
				let that = this;
				that.setWhere();
				if (that.loadend) return;
				if (that.loading) return;
				if (isPage === true) that.$set(that, 'productList', []);
				that.loading = true;
				that.loadTitle = '';
				getProductslist(that.where).then(res => {
					let list = res.data.list;
					// 为商品添加标签和描述
					list = list.map(item => {
						// 添加示例标签
						item.tags = ['热销', '高精度', '自动化', '灵活', '产线', '模块化', '检测', '品质'].slice(0, Math.floor(Math.random() * 4) + 2);
						// 添加示例描述
						item.info = item.info
						// 随机添加新品标签
						item.isNew = Math.random() > 0.7;
						return item;
					});
					let productList = that.$util.SplitArray(list, that.productList);
					let loadend = list.length < that.where.limit;
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? '已全部加载' : '加载更多';
					that.$set(that, 'productList', productList);
					that.$set(that.where, 'page', that.where.page + 1);
					if (that.productList.length === 0) {
						this.get_host_product();
					} 
				}).catch(err => {
					that.loading = false;
					that.loadTitle = '加载更多';
				});
			},
		},
		onReachBottom() {
			if (this.productList.length > 0) {
				this.get_product_list();
			} else {
				this.$refs.recommendIndex.get_host_product();
			}
		}
	}
</script>

<style scoped lang="scss">
	.bg_color{
		@include main_bg_color(theme);
	}
	.font_color{
		@include main_color(theme);
	}
	.x-money{
		@include price_color(theme);
	}
	.iconfont {
		color: #fff;
	}
    .listBox{
		padding: 20px 15px;
		margin-top: 154rpx;
	}
	.productList .search {
		width: 100%;
		height: 86rpx;
		padding-left: 23rpx;
		box-sizing: border-box;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 9;
	}

	.productList .search .input {
		// width: 640rpx;
		height: 60rpx;
		background-color: #fff;
		border-radius: 50rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.productList .search .input input {
		/* #ifdef H5 */
		width: 528rpx;
		/* #endif */
		/* #ifndef H5 */
		width: 548rpx;
		/* #endif */
		height: 100%;
		font-size: 26rpx;
	}

	.productList .search .input .placeholder {
		color: #999;
	}

	.productList .search .input .iconfont {
		font-size: 35rpx;
		color: #555;
	}

	.productList .search .icon-pailie,
	.productList .search .icon-tupianpailie,
	.productList .search .icon-liebiao,
	.productList .search .view-toggle {
		color: #fff;
		width: 62rpx;
		font-size: 40rpx;
		height: 86rpx;
		line-height: 86rpx;
		cursor: pointer;
	}

	.productList .nav {
		height: 86rpx;
		color: #454545;
		position: fixed;
		left: 0;
		width: 100%;
		font-size: 28rpx;
		background-color: #fff;
		margin-top: 86rpx;
		top: 0;
		z-index: 9;
	}

	.productList .nav .item {
		width: 25%;
		text-align: center;
	}

	.productList .nav .item.font-color {
		font-weight: bold;
	}

	.productList .nav .item image {
		width: 15rpx;
		height: 19rpx;
		margin-left: 10rpx;
	}

	/* 新的商品网格布局样式 */
	.product-grid-container {
		padding: 20rpx 15rpx;
		margin-top: 192rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.product-grid {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 20rpx;
	}

	.product-card {
		background-color: #fff;
		border-radius: 12rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
		overflow: hidden;
		position: relative;
	}

	.product-image-container {
		position: relative;
		width: 100%;
		height: 200rpx;
		background-color: #f0f0f0;
	}

	.product-image {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.new-tag {
		position: absolute;
		top: 10rpx;
		left: 10rpx;
		background-color: #ff4444;
		color: #fff;
		font-size: 20rpx;
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
		z-index: 2;
	}

	.activity-tag {
		position: absolute;
		top: 10rpx;
		right: 10rpx;
		background-color: #ff6b35;
		color: #fff;
		font-size: 20rpx;
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
		z-index: 2;
	}

	.product-info {
		padding: 20rpx;
	}

	.product-name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.product-desc {
		font-size: 22rpx;
		color: #666;
		margin-bottom: 15rpx;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.product-tags {
		display: flex;
		flex-wrap: wrap;
		gap: 8rpx;
		margin-bottom: 20rpx;
	}

	.tag {
		background-color: #f0f0f0;
		color: #666;
		font-size: 20rpx;
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
	}

	.product-actions {
		display: flex;
		gap: 10rpx;
	}

	.action-btn {
		flex: 1;
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		font-size: 24rpx;
		border-radius: 8rpx;
	}

	.inquiry-btn {
		background-color: #3cc51f;
		color: #fff;
	}

	.quote-btn {
		background-color: #fff;
		color: #3cc51f;
		border: 2rpx solid #3cc51f;
	}

	.noCommodity {
		background-color: #fff;
		padding-bottom: 30rpx;
		margin-top: 172rpx;
	}

	/* 列表视图样式 */
	.product-grid-container.list-view {
		padding: 20rpx 15rpx;
	}

	.product-grid.list-grid {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}

	.product-card.list-card {
		display: flex;
		flex-direction: row;
		height: 200rpx;
	}

	.list-card .product-image-container {
		width: 200rpx;
		height: 100%;
		flex-shrink: 0;
	}

	.list-card .product-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 20rpx;
	}

	.list-card .product-name {
		font-size: 28rpx;
		margin-bottom: 8rpx;
		-webkit-line-clamp: 1;
		line-clamp: 1;
	}

	.list-card .product-desc {
		font-size: 22rpx;
		margin-bottom: 10rpx;
		-webkit-line-clamp: 2;
		line-clamp: 2;
	}

	.list-card .product-tags {
		margin-bottom: 10rpx;
	}

	.list-card .product-actions {
		justify-content: flex-end;
		gap: 15rpx;
	}

	.list-card .action-btn {
		flex: none;
		width: 120rpx;
	}
</style>
